package Z06XianCheng.XC05DealSyn;

import Z06XianCheng.XC01StartXianCheng.Hero;
// 死锁测试成功

public class dealsyn {
    public static void main(String[] args) {
        final Hero ahri = new Hero();
        ahri.name = "九尾妖狐";
        final Hero annie = new Hero();
        annie.name = "安妮";

        Thread t1 = new Thread(){
          public void run(){
              synchronized (ahri){
                  System.out.println("t1 占有九尾妖狐");

                  try{
                      Thread.sleep(1000);
                  } catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  System.out.println("t1 试图占有安妮");
                  System.out.println("t1 等待中。。。");
                  synchronized (annie){
                      System.out.println("占据安妮成功~！");
                  }
              }
          }
        };
        t1.start();


        Thread t2 = new Thread(){
            public void run(){
                synchronized (annie){
                    System.out.println("t2 占有安妮");

                    try{
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("t2 试图占有九尾狐");
                    System.out.println("t2 等待中。。。");
                    synchronized (ahri){
                        System.out.println("占据九尾狐成功~！");
                    }
                }
            }
        };
        t2.start();
    }
}
